package hcqs.nav.grid.map.api;

import hcqs.nav.grid.macros.Cell;
import hcqs.nav.grid.macros.Point;
import hcqs.nav.grid.macros.Pose;

import java.util.List;

public abstract class Layer {
    public abstract void saveMap(String mapId);

    public abstract void addPartialMap(String mapId, Pose agvPose, List<Point> obstacles);

    public abstract void refreshMap(String mapId, Point leftTopPoint, byte[][] map);

    public abstract byte[][] getMapArea(Point ltPoint, int width, int height);

    public abstract void changeMap(String mapId);

    public abstract void updateMap(Point ltPoint, int width, int height, byte[][] map);

    public void createMap(String mapId) {
        this.mapId = mapId;
        isCreateMap = true;
    }

    public boolean createMapComplete(String mapId) {
        if (mapId == null || !mapId.equals(this.mapId))
            return false;
        isCreateMap = false;
        saveMap(mapId);
        return true;
    }

    public Cell worldToCellX(Point worldPoint) {
        Cell cell = new Cell();
        cell.setX((worldPoint.getX() - leftTopPoint.getX()) / resolution);
        cell.setY((leftTopPoint.getY() - worldPoint.getY())/ resolution);
        return cell;
    }

    private Cell cellMin = new Cell(0, 0);
    private Cell cellMax = new Cell(0, 0);
    private Point worldMin = new Point(0, 0);
    private Point worldMax = new Point(0, 0);
    private Point leftTopPoint = new Point(0, 0);
    private int resolution = 10;

    private final Object lock = new Object();
    private boolean isCreateMap = false;

    private String mapId;

    public Cell getCellMin() {
        return cellMin;
    }

    public void setCellMin(Cell cellMin) {
        this.cellMin = cellMin;
    }

    public Cell getCellMax() {
        return cellMax;
    }

    public void setCellMax(Cell cellMax) {
        this.cellMax = cellMax;
    }

    public Point getWorldMin() {
        return worldMin;
    }

    public void setWorldMin(Point worldMin) {
        this.worldMin = worldMin;
    }

    public Point getWorldMax() {
        return worldMax;
    }

    public void setWorldMax(Point worldMax) {
        this.worldMax = worldMax;
    }

    public Point getLeftTopPoint() {
        return leftTopPoint;
    }

    public void setLeftTopPoint(Point leftTopPoint) {
        this.leftTopPoint = leftTopPoint;
    }

    public boolean isCreateMap() {
        return isCreateMap;
    }

    public void setCreateMap(boolean createMap) {
        isCreateMap = createMap;
    }

    public String getMapId() {
        return mapId;
    }

    public void setMapId(String mapId) {
        this.mapId = mapId;
    }

    public int getResolution() {
        return resolution;
    }

    public void setResolution(int resolution) {
        if (resolution <= 0)
            return;
        this.resolution = resolution;
    }

    public Object getLock() {
        return lock;
    }
}
